# -*- coding: utf-8 -*-


import os
import shutil
import string

from global_data.constant_data import ConstantData


class FileUtils(object):
    @staticmethod
    def get_filename(file_path, isext=True):
        if isext:
            return os.path.basename(file_path)
        else:
            return os.path.splitext(os.path.basename(file_path))[0]

    @staticmethod
    def mkdir(path):
        if not FileUtils.dir_exists(path):
            try:
                os.mkdir(FileUtils.get_new_path(path))
            except FileExistsError:
                pass

    @staticmethod
    def mkdirs(path):
        if not FileUtils.file_exists(path):
            try:
                os.makedirs(FileUtils.get_new_path(path))
            except FileExistsError:
                pass

    @staticmethod
    def dir_exists(path):
        path = FileUtils.get_new_path(path)
        return os.path.exists(path)

    @staticmethod
    def file_exists(path):
        path = FileUtils.get_new_path(path)
        return os.path.isfile(path)

    @staticmethod
    def read_file(path):
        path = FileUtils.get_new_path(path)
        if FileUtils.file_exists(path):
            with open(path, "r", encoding="utf-8") as f:
                data = f.read()
            return data
        else:
            raise Exception(f"文件不存在")

    @staticmethod
    def join_path(*path):
        """
        拼接路径
        :param path
        :return:
        """
        return os.path.join(*path).replace('\\', '/')

    @staticmethod
    def get_ui_path(path):
        # path = FileUtils.get_new_path("windows", path)
        if not path.endswith(".ui"):
            path = path + ".ui"
        return f"windows/{path}"

    @staticmethod
    def get_new_path(*paths):
        if len(paths) == 1:
            path = paths[0]
            if FileUtils.is_absolute_path(path):
                return path
            return FileUtils.join_path(ConstantData.WORK_PATH, path)
        else:
            temp = ConstantData.WORK_PATH
            for path in paths:
                temp = FileUtils.join_path(temp, path)
            return temp

    @staticmethod
    def is_absolute_path(path):
        return os.path.isabs(path)

    @staticmethod
    def remove_file_by_path(folder_path):
        path = FileUtils.get_new_path(folder_path)
        if FileUtils.dir_exists(path):
            shutil.rmtree(path)

    @staticmethod  # 打开文件或者速度最快, 推荐，不过只适用于Windows
    def start_file(file_path):
        os.startfile(file_path)

    @staticmethod
    def copy_file(src_path, dst_path):
        shutil.copyfile(src_path, dst_path)

    @staticmethod
    def copy(src_path, dst_path):
        shutil.copy(src_path, dst_path)

    @staticmethod
    def copy2(src_path, dst_path):
        shutil.copy2(src_path, dst_path)

    @staticmethod
    def copytree(src_path, dst_path, dirs_exist_ok=True):
        shutil.copytree(src_path, dst_path, dirs_exist_ok)

    @staticmethod
    def rmtree(folder_path):
        shutil.rmtree(folder_path)
        # os.mkdir(folder_path)

    @classmethod
    def listdir(cls, path, type="all"):
        """
        获取目录下的所有目录及文件，不含子目录
        :param path:
        :param type:
        :return:
        """
        if not FileUtils.dir_exists(path):
            return []
        filelist = os.listdir(path)
        if type == "all":
            return filelist
        res = []
        for file in filelist:
            file_path = os.path.join(path, file)
            if type == "dir" and os.path.isdir(file_path):
                res.append(file)
            elif type == "file" and os.path.isfile(file_path):
                res.append(file)
        return res

    @classmethod
    def walk(cls, path):
        """
        返回一个三元组，其中包含当前遍历的目录名、当前目录下的所有子目录名和当前目录下的所有文件名。
        :param path:
        :return:
        """
        # for root, dirs, files in os.walk(path):
        #     print("当前目录：", root)
        #     print("子目录列表：", dirs)
        #     print("文件列表：", files)
        return os.walk(path)


if __name__ == '__main__':
    print("Python")
